package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

public class PhotoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OpenPictureListener {

        void openPicture(int position);

        void isSelect(int position);

    }

    private OpenPictureListener mOpenPictureListener;

    private ImageView mIvContent;

    private ImageView mIvSelect;

    private Context mContext;

    public PhotoViewHolder(Context context, View itemView) {
        super(itemView);
        mContext = context;
        mIvContent = (ImageView) itemView.findViewById(R.id.picture_content);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int size = (int) ((metrics.widthPixels - ChZhRJUtils.dp2px(5, metrics) * 5) / 4);
        mIvContent.setLayoutParams(new RelativeLayout.LayoutParams(size, size));
        mIvSelect = (ImageView) itemView.findViewById(R.id.picture_select);
        mIvContent.setOnClickListener(this);
        mIvSelect.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.picture_content:
                openPicture();
                break;
            case R.id.picture_select:
                isSelect();
                break;
        }
    }

    public void setOpenPictureListener(OpenPictureListener openPictureListener) {
        mOpenPictureListener = openPictureListener;
    }

    public ImageView getPicture() {
        return mIvContent;
    }

    private void openPicture() {
        if (mOpenPictureListener != null) {
            mOpenPictureListener.openPicture(getPosition());
        }
    }

    public void setSelect(boolean isSelect) {
        if (isSelect) {
            mIvSelect.setImageDrawable(mContext.getResources().getDrawable(R.drawable.btn_picture_select_2));
        } else {
            mIvSelect.setImageDrawable(mContext.getResources().getDrawable(R.drawable.btn_picture_select_1));
        }
    }

    public void isSelect() {
        if (mOpenPictureListener != null) {
            mOpenPictureListener.isSelect(getPosition());
        }
    }

}
